// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <string>
#include <stack>

struct exp_type
{
	exp_type(int num) : num(num) { has_value = true; is_string = is_bask = false; }
	exp_type(std::string s) : s(s) { is_string = has_value = true; is_bask = false; }
	exp_type() { has_value = false; }

	int value() const
	{
		if (is_bask) return 0;
		if (!is_string) return num;
		return std::stoi(s);
	}
	bool empty() { return !has_value; }
	bool is_number() const { return !is_string && !is_bask; }
	bool is_basket() const { return is_bask; }

	void to_neg_value()
	{
		if (!is_string) { num = -num; return; }
		is_string = false;
		num = std::stoi(s);
		num = -num;
	}

	void set_bask()
	{
		is_string = false; is_bask = true;
	}

	void set_value(int x)
	{
		is_string = is_bask = false;
		num = x;
	}

	void to_value()
	{
		if (is_bask) {
			num = 0;
			is_bask = is_string = false;
			return;
		}
		if (!is_string) return;
		is_string = false;
		if (s.empty()) num = 0;
		else num = std::stoi(s);
	}

	void to_string()
	{
		if (is_bask) {
			is_bask = false; is_string = true;
			s = "";
			return;
		}
		if (is_string) return;
		is_string = true;
		s = std::to_string(num);
	}

	void to_rev_value()
	{
		if (is_bask) {
			is_string = is_bask = false;
			num = 0;
			return;
		}
		if (is_string) {
			is_string = false;
			num = s.empty();
		} else
			num = !num;
	}

private:
	bool is_string;
	bool has_value;
	bool is_bask;
	std::string s;
	int num;
};

exp_type operator*(exp_type const & a, exp_type const & b)
{
	return exp_type(a.value() * b.value());
}

exp_type operator+(exp_type const & a, exp_type const & b)
{
	if (!a.is_number() || !b.is_number())
		return exp_type(std::to_string(a.value()) + std::to_string(b.value()));
	return exp_type(a.value() + b.value());
}

exp_type operator-(exp_type const & a, exp_type const & b)
{
	return exp_type(a.value() - b.value());
}

std::string s;

exp_type calc(std::string const & s);

int get_match(std::string const & s, int start)
{
	int tot = 0;
	for (int i = start; i < (int)s.size(); i++) {
		if (s[i] == '[') tot++;
		else if (s[i] == ']') tot--;
		if (!tot) return i;
	}
	return -1;
}

exp_type get_next_exp(std::string const & s, int & start)
{
	exp_type ret;
	int len = s.size();
	std::stack<char> single;
	for (int i = start; i < len; i++) {
		if (s[i] == '!') single.push('!');
		else if (s[i] == '-') {
			if (ret.empty()) single.push('-');
			else { start = i - 1; return ret; }
		} else if (s[i] == '+') {
			if (ret.empty()) continue;
			start = i - 1;
			return ret;
		} else if (s[i] == '[') {
			int r = get_match(s, i);
			exp_type tmp("");
			tmp.set_bask();
			if (i + 1 < r)
				tmp = calc(std::string(s.begin() + i + 1, s.begin() + r));
			if (r + 3 < len && s[r + 1] == '[') {
				int tr = get_match(s, r + 1);
				i = tr;
			} else {
				i = r;
				if (!tmp.is_basket()) tmp.to_string();
			}
			while (!single.empty()) {
				if (single.top() == '-') tmp.to_neg_value();
				else tmp.to_rev_value();
				single.pop();
			}
			ret = tmp;
		} else if (s[i] == '*') {
			auto tmp = get_next_exp(s, ++i);
			ret = ret * tmp;
		}
	}
	start = len - 1;
	return ret;
}

exp_type calc(std::string const & s)
{
	std::stack<exp_type> exp;
	bool rev = false;
	bool has_rev = false;
	int len = s.size();
	std::stack<char> single;
	for (int i = 0; i < len; i++) {
		if (s[i] == '+') {
			if (exp.empty()) {
				auto tmp = get_next_exp(s, ++i);
				tmp.to_value();
				if (has_rev) {
					if (rev) tmp.to_rev_value();
					else if (tmp.is_basket()) tmp.set_value(1);
					has_rev = rev = false;
				}
				exp.push(tmp);
			} else {
				auto tmp = get_next_exp(s, ++i);
				auto t2 = exp.top(); exp.pop();
				t2 = t2 + tmp;
				exp.push(t2);
			}
		} else if (s[i] == '-') {
			if (exp.empty()) {
				auto tmp = get_next_exp(s, ++i);
				tmp.to_neg_value();
				if (has_rev) {
					if (rev) tmp.to_rev_value();
					else if (tmp.is_basket()) tmp.set_value(1);
					has_rev = rev = false;
				}
				exp.push(tmp);
			} else {
				auto tmp = get_next_exp(s, ++i);
				auto t2 = exp.top(); exp.pop();
				t2 = t2 - tmp;
				exp.push(t2);
			}
		} else if (s[i] == '*') {
			auto tmp = get_next_exp(s, ++i);
			auto t2 = exp.top(); exp.pop();
			t2 = t2 * tmp;
			exp.push(t2);
		} else if (s[i] == '!') {
			has_rev = true;
			rev ^= true;
		} else if (s[i] == '[') {
			int r = get_match(s, i);
			exp_type tmp("");
			tmp.set_bask();
			if (i + 1 < r)
				tmp = calc(std::string(s.begin() + i + 1, s.begin() + r));
			if (r + 3 < len && s[r + 1] == '[') {
				int tr = get_match(s, r + 1);
				i = tr;
			} else {
				i = r;
				if (!tmp.is_basket()) tmp.to_string();
			}
			if (has_rev) {
				if (rev) tmp.to_rev_value();
				else if (tmp.is_basket()) tmp.set_value(1);
				has_rev = rev = false;
			}
			exp.push(tmp);
		}
	}
	return exp.top();
}

int main()
{
	int T; std::cin >> T;
	while (T--) {
		std::cin >> s;
		std::cout << calc(s).value() << '\n';
	}
}

